//
//  SinaNewFeatureCollectionViewController.swift
//  weibo
//
//  Created by sirius on 16/1/4.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
let count = 4

class SinaNewFeatureCollectionViewController: UICollectionViewController {
    //全局变量
    let flowLayout = UICollectionViewFlowLayout()
    required init(){
        super.init(collectionViewLayout: flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
         super.viewDidLoad()
        setupUI()
    }
    //设置子控件
    private func setupUI(){
    //注册系统的cell
    //UICollectionViewCell.self   代替class
    collectionView?.registerClass(SinaNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier:reuseIdentifier)
    //设置分页
    collectionView?.pagingEnabled = true
    collectionView?.bounces = false
        
    //关闭导航条
    collectionView?.showsHorizontalScrollIndicator = false
    collectionView?.showsHorizontalScrollIndicator = false
    //设置item 的 大小 1.先得获取布局 2.设置方向和布局 item的间距
    flowLayout.itemSize = UIScreen.mainScreen().bounds.size
    flowLayout.scrollDirection  = UICollectionViewScrollDirection.Horizontal
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.minimumLineSpacing = 0
    }
}
extension SinaNewFeatureCollectionViewController    {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return count
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SinaNewFeatureCollectionViewCell
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        cell.index = indexPath.item
        return cell
    }
}
class SinaNewFeatureCollectionViewCell: UICollectionViewCell{
    //设置模型
    var index: Int = 0 {
    didSet{
        iconImageView.image = UIImage(named: "new_feature_\(index + 1)")
        if index == 3{
            enterButton.hidden = false
        }else{
            enterButton.hidden = true
        }
    }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()   
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var iconImageView: UIImageView = {
        let img = UIImageView()
        return img
    }()
    lazy var enterButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置标题
        button.setTitle("立即进入", forState: UIControlState.Normal)
        //文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        button.addTarget(self, action: "enter", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //MARK:---按钮点击方法
    func enter(){
        printLog("点击进入")
        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEnter, object: nil)
    }
    //MARK:---设置子控件和给子控件添加约束
    private func setupUI(){
        //添加子控件
        addSubview(iconImageView)
        addSubview(enterButton)
        //设置约束 --- 直接设置 frame
        iconImageView.frame = bounds
        
        //设置约束 水平居中，距离cell的底部 150
        enterButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -150))
    }
}
